let uuid = require('node-uuid')
let mysql = require('./connection.js')

module.exports = {
  addmenu: function (data) {
    return new Promise((resolve, reject) => {
      mysql.query('select count(*) as count from bas_report_menu;').then(result => {
        if (result[0].count > 0) {
          mysql.query(`update bas_report_menu set menu_data='${JSON.stringify(data)}'`).then(result => {
            resolve({success: true})
          })
        } else {
          mysql.query(`insert into bas_report_menu(id,menu_data) values('${uuid.v1()}','${JSON.stringify(data)}')`)
            .then(result => {
              resolve({success: true})
            })
        }
      })
    })
  },
  addconfig: function (data) {
    return new Promise((resolve, reject) => {
      mysql.query(`select count(*) as count from bas_report_config where id='${data.id}';`).then(result => {
        if (result[0].count > 0) {
          mysql.query(`update bas_report_config set config_data='${JSON.stringify(data.config)}' where id= '${data.id}';`).then(result => {
            resolve({success: true})
          })
        } else {
          mysql.query(`insert into bas_report_config(id,config_data) values('${data.id}','${JSON.stringify(data.config)}')`)
            .then(result => {
              resolve({success: true})
            })
        }
      })
    })
  },
  getmenu: function () {
    return new Promise((resolve, reject) => {
      mysql.query(`select * from bas_report_menu;`).then(result => {
        if (result.length > 0) {
          resolve(JSON.parse(result[0].menu_data))
        } else {
          resolve(undefined)
        }
      })
    })
  },
  getconfig: function (data) {
    return new Promise((resolve, reject) => {
      mysql.query(`select * from bas_report_config where id='${data.id}';`).then(result => {
        if (result.length > 0) {
          resolve(JSON.parse(result[0].config_data))
        } else {
          resolve(undefined)
        }
      })
    })
  }
}

const menu = {
  edit: false,
  list: [
    {
      code: '01',
      name: 'XX类报表',
      children: [
        {
          id: 'cedc3974b7e411e787d60050c2273f5f',
          name: 'XX用电量日报表'
        },
        {
          id: 'aaaaaaaaaxxxxx2',
          name: 'XX用电量周报表'
        },
        {
          id: 'aaaaaaaaaxxxxx3',
          name: 'XX用电量年报表'
        }
      ]
    },
    {
      code: '02',
      name: 'YY类报表',
      children: [
        {
          id: 'bbbbbbbbbxxxxx1',
          name: 'YY用电量日报表'
        },
        {
          id: 'bbbbbbbbbxxxxx2',
          name: 'YY用电量周报表'
        },
        {
          id: 'bbbbbbbbbxxxxx3',
          name: 'YY用电量年报表'
        }
      ]
    }
  ]
}

const config = {
  name: 'XX用电量日报表',
  dateFormat: 'hour', // year:只选年，month：选到月份，day：选到日，hour：选到小时，minute：选到分钟，second：选到秒
  interval: 100, // 数据接口请求间隔时间，以秒为单位，如果为0，则只请求一次
  items: [
    {
      type: 'text',
      pos: {x: 400, y: 10, w: 200, h: 40},
      font: {size: 10, color: '#454545', align: 'center'},
      data: 'B25冷冻站每日冷电量对比'
    },
    {
      type: 'text',
      pos: {x: 10, y: 10, w: 80, h: 36},
      font: {size: 10, color: '#454545', align: 'center'},
      data: '年度'
    },
    {
      type: 'point',
      pos: {x: 10, y: 30, w: 80, h: 20},
      font: {size: 10, color: '#454545', align: 'center'},
      data: 'total',
      src: {
        total: {type: 'cal', name: '1VAR24', func: 'sum'}
      }
    },
    {
      type: 'point',
      pos: {x: 10, y: 50, w: 80, h: 20},
      font: {size: 10, color: '#454545', align: 'center'},
      data: 'total1',
      src: {
        total1: {type: 'real', name: '1VAR24'}
      }
    },
    {
      type: 'point',
      pos: {x: 10, y: 70, w: 80, h: 20},
      font: {size: 10, color: '#454545', align: 'center'},
      data: 'total'
    },
    {
      type: 'table',
      pos: {x: 10, y: 90, w: 1500, h: 800},
      data: 'list',
      src: {
        date: {type: 'date', interval: 60 * 60},
        v1: {type: 'real', name: '1VAR24'},
        v1min: {type: 'cal', name: '1VAR24', func: 'min'},
        v1avg: {type: 'cal', name: '1VAR24', func: 'avg'},
        v1max: {type: 'cal', name: '1VAR24', func: 'max'},
        v2min: {type: 'cal', name: '1VAR24', func: 'min'},
        v2avg: {type: 'cal', name: '1VAR24', func: 'avg'},
        v2max: {type: 'cal', name: '1VAR24', func: 'max'},
        v3min: {type: 'cal', name: '1VAR24', func: 'min'},
        v3avg: {type: 'cal', name: '1VAR24', func: 'avg'},
        v3max: {type: 'cal', name: '1VAR24', func: 'max'}
      },
      cols: [
        {name: '日期', field: 'date', fixed: true},
        {name: 'v1-用电量',
          nodes: [
            {name: '最小值', field: 'v1min'},
            {name: '平均值', field: 'v1avg'},
            {name: '最大值', field: 'v1max'}
          ]
        },
        {name: 'v2-用电量',
          nodes: [
            {name: '最小值', field: 'v2min'},
            {name: '平均值', field: 'v2avg'},
            {name: '最大值', field: 'v2max'}
          ]
        },
        {name: 'v3-用电量',
          nodes: [
            {name: '最小值', field: 'v3min'},
            {name: '平均值', field: 'v3avg'},
            {name: '最大值', field: 'v3max'}
          ]
        }
      ]
    }
  ]
}
